package com.rds.benchmark.operator;

import com.rds.benchmark.util.MakeValue;
import org.redisson.api.RBucket;

import java.io.IOException;

public class StringRedissonOperator extends TypeOperatorBase {

    private String key;
    private RBucket<Object> conn;

    private final String value = MakeValue.value;

    @Override
    public void init(Object conn, String key, int idx,String mode) {
        this.conn = (RBucket<Object>) conn;
        this.key = key;
    }

    @Override
    public Long clear() {
        return conn.delete() ? 1L : 0L;
    }

    @Override
    public void operate() throws IOException {
        conn.set(value);

        String msg = (String) conn.get();
        if (!value.equals(msg)) {
            times +=2;
            throw new IOException("set value = '" + value + "', but get value = '" + msg + "'");
        }
        else {
            conn.delete();
            times +=3;
        }
    }
}
